[CF776B]Sherlock and his girlfriend

题目

题目描述

Sherlock has a new girlfriend (so unlike him!). Valentine’s day is coming and he wants to gift her some jewelry.

He bought n pieces of jewelry. The i-th piece has price equal to i + 1, that is, the prices of the jewelry are 2, 3, 4, … n + 1.

Watson gave Sherlock a challenge to color these jewelry pieces such that two pieces don’t have the same color if the price of one piece is a prime divisor of the price of the other piece. Also, Watson asked him to minimize the number of different colors used.

Help Sherlock complete this trivial task.

输入

The only line contains single integer n (1 ≤ n ≤ 100000) — the number of jewelry pieces.

输出

The first line of output should contain a single integer k, the minimum number of colors that can be used to color the pieces of jewelry with the given constraints.

The next line should consist of n space-separated integers (between 1 and k) that specify the color of each piece in the order of increasing price.

If there are multiple ways to color the pieces using k colors, you can output any of them.

样例输入输出

样例1

输入

1
3

输出

1
2
2
1 1 2

样例2

输入

1
4

输出

1
2
2
2 1 1 2

解释

In the first input, the colors for first, second and third pieces of jewelry having respective prices 2, 3 and 4 are 1, 1 and 2 respectively.

In this case, as 2 is a prime divisor of 4, colors of jewelry having prices 2 and 4 must be distinct.

题解

显然如果$i + 1$是一个质数,那么只有价格为$i + 1$的倍数的珠宝必须具有不同的颜色
所以我们枚举$2$到$n+1$,并且如果枚举到的珠宝没涂色,说明是个质数,让他的倍数涂色
最后我们留下来的就是质数和非质数,而质数之间不可能互为质因数,非质数之间也不能为质因数,所以只要你将他们分别输出不同的数字就行

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<cstdio>
using namespace std;
bool s[100005];

int main(){
int n;
cin>>n;
for(int i=2;i<=n+1;i++){
if(!s[i]){
for(int j=i<<1;j<=n+1;j+=i)s[j]=1;
}
}
if(n>2)cout<<"2\n";
else cout<<"1\n";
for(int i=2;i<=n+1;i++){
if(!s[i])cout<<"1 ";
else cout<<"2 ";
}
return 0;
}